package com.ruoyi.gcgl.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.gcgl.mapper.GysptMaterialTypeMapper;
import com.ruoyi.gcgl.domain.GysptMaterialType;
import com.ruoyi.gcgl.service.IGysptMaterialTypeService;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.StringUtils;

/**
 * 材料分类Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-02
 */
@Service
public class GysptMaterialTypeServiceImpl implements IGysptMaterialTypeService 
{
    @Autowired
    private GysptMaterialTypeMapper gysptMaterialTypeMapper;

    /**
     * 查询材料分类
     * 
     * @param id 材料分类主键
     * @return 材料分类
     */
    @Override
    public GysptMaterialType selectGysptMaterialTypeById(Long id)
    {
        return gysptMaterialTypeMapper.selectGysptMaterialTypeById(id);
    }

    /**
     * 查询材料分类列表
     * 
     * @param gysptMaterialType 材料分类
     * @return 材料分类
     */
    @Override
    public List<GysptMaterialType> selectGysptMaterialTypeList(GysptMaterialType gysptMaterialType)
    {
        return gysptMaterialTypeMapper.selectGysptMaterialTypeList(gysptMaterialType);
    }

    /**
     * 新增材料分类
     * 
     * @param gysptMaterialType 材料分类
     * @return 结果
     */
    @Override
    public int insertGysptMaterialType(GysptMaterialType gysptMaterialType)
    {
        return gysptMaterialTypeMapper.insertGysptMaterialType(gysptMaterialType);
    }

    /**
     * 修改材料分类
     * 
     * @param gysptMaterialType 材料分类
     * @return 结果
     */
    @Override
    public int updateGysptMaterialType(GysptMaterialType gysptMaterialType)
    {
        return gysptMaterialTypeMapper.updateGysptMaterialType(gysptMaterialType);
    }

    /**
     * 批量删除材料分类
     * 
     * @param ids 需要删除的材料分类主键
     * @return 结果
     */
    @Override
    public int deleteGysptMaterialTypeByIds(String ids)
    {
        return gysptMaterialTypeMapper.deleteGysptMaterialTypeByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除材料分类信息
     * 
     * @param id 材料分类主键
     * @return 结果
     */
    @Override
    public int deleteGysptMaterialTypeById(Long id)
    {
        return gysptMaterialTypeMapper.deleteGysptMaterialTypeById(id);
    }

	@Override
	@DataScope(deptAlias = "d")
	public List<Ztree> selectTypeTree(GysptMaterialType type) {
		// TODO Auto-generated method stub
		List<GysptMaterialType> typeList = gysptMaterialTypeMapper.selectGysptMaterialTypeList(type);
        List<Ztree> ztrees = initZtree(typeList);
		return ztrees;
	}
	
	/**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<GysptMaterialType> deptList)
    {
        return initZtree(deptList, null);
    }
    
    /**
     * 对象转部门树
     *
     * @param deptList 部门列表
     * @param roleDeptList 角色已存在菜单列表
     * @return 树结构列表
     */
    public List<Ztree> initZtree(List<GysptMaterialType> typeList, List<String> roleDeptList){
        List<Ztree> ztrees = new ArrayList<Ztree>();
        for (GysptMaterialType type : typeList){
            Ztree ztree = new Ztree();
            ztree.setId(type.getId());
            ztree.setpId(type.getpId());
            ztree.setName(type.getName());
            ztree.setTitle(type.getName());
            ztrees.add(ztree);
        }
        return ztrees;
    }
}